package com.cssw.fyzb.tps.api.im;

import com.cssw.fyzb.tps.service.im.GroupService;
import com.cssw.fyzb.tps.service.im.common.ImHttpComm;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Jian Qin.
 */
@Function(description = "创建群组", parameters = {
        @Parameter(name = "owner_Account", type = Type.String, description = "群主 ID"),
        @Parameter(name = "groupId", type = Type.String, description = "群组 ID", required = false),
        @Parameter(name = "type", type = Type.String, description = "群组形态，Public（陌生人社交群），Private（好友工作群），ChatRoom（会议群），AVChatRoom（直播群）"),
        @Parameter(name = "name", type = Type.String, description = "群名称"),
        @Parameter(name = "introduction", type = Type.String, description = "群简介", required = false),
        @Parameter(name = "notification", type = Type.String, description = "群公告", required = false),
        @Parameter(name = "faceUrl", type = Type.String, description = "群头像 URL", required = false),
        @Parameter(name = "maxMemberCount", type = Type.Integer, description = "最大群成员数量", required = false),
})
@Component
public class CreateGroup implements Action {
    @Autowired
    private GroupService groupService;

    @Override
    public Response execute(Request request) throws Exception {
        ImHttpComm.ImResult result = groupService.createGroup(
                request.getString("owner_Account"),
                request.getString("groupId"),
                request.getString("type"),
                request.getString("name"),
                request.getString("introduction"),
                request.getString("notification"),
                request.getString("faceUrl"),
                request.getInteger("maxMemberCount"));
        if(result.getErrorCode() == 0){
            return Response.success().put("groupId", result.getString("GroupId"));
        }else{
            return Response.fail(result.getErrorInfo(), result.getErrorCode());
        }
    }
}
